import Message from './message.vue'

export default {
  // @ts-ignore
  install (Vue, options) {
  //   const VueMsg = Vue.extend(Message)
  //   let vm: { hide: () => void; message: any; type: any; show: () => void; } | null = null
  //   const ShowMessage = ({ message, type, time=3000}) => {
  //     return new Promise(resolve => {
  //       if(!vm){
  //         vm = new VueMsg()
  //         vm.$mount()
  //         document.querySelector('body').appendChild(vm.$el)
  //       }else {
  //         vm.hide()
  //         clearTimeout(ShowMessage.timer)
  //       }
  //       vm.message = message;
  //       vm.type = type
  //       vm.show();
  //     })
  //   }
  // }

    const VueMsg = Vue.extend(Message)
    let vm: { $mount: () => void; $el: any; hide: () => void; message: any; type: any; show: () => void; } | null = null
    vm = new VueMsg()
    // @ts-ignore
    vm.$mount()
    // @ts-ignore
    document.body.appendChild(vm.$el)

    let timer: number | undefined

    // const ShowMessage = ({ message, type, time = 2000 }) => {
    //     vm.message = message;
    //     vm.type = type;
    //     vm.show();

    //     setTimeout(() => {
    //         // vm.hide();
    //         vm.show();
    //     }, time);
    // };
    // Vue.Message = Vue.prototype.$message = ShowMessage;
    // Vue.prototype.$message = ShowMessage;

    const ShowMessage = {
      all_message (message: any, type: any, duration: number | undefined) {
        clearTimeout(timer)
        timer = setTimeout(() => {
          // @ts-ignore
          vm.hide()
        }, duration)
        // @ts-ignore
        vm.message = message
        // @ts-ignore
        vm.type = type
        // @ts-ignore
        vm.show()
      },
      success (message: any, type: any, duration = 2000) {
        this.all_message(message, type, duration)
      },
      error (message: any, type: any, duration = 2000) {
        this.all_message(message, type, duration)
      }
    }
    // 将方法挂载全局
    Vue.prototype.$message = ShowMessage
  }
}
